package com.zyl.blog.common.enums;

import lombok.Getter;

import java.util.Set;

/**
 * 文件上传策略枚举
 * PS: 根据业务需求扩展策略
 * @version v1.0
 * @ProjectName: Personal_Blog
 * @Package: com.zyl.common.core.enums
 * @ClassName: UploadStrategy
 * @Description: 文件上传策略枚举
 * @Author FinalFantasy
 * @Date 2025/3/10-22:14
 */
@Getter
//@Schema(description = "文件上传策略枚举（OSS兼容版）")
public enum UploadStrategy {
    //@Schema(description = "图片文件：支持主流图片格式，最大10MB")
    IMAGE(
            "static/images",
            Set.of(
                    "image/jpeg",       // JPG/JPEG
                    "image/png",        // PNG
                    "image/gif",        // GIF
                    "image/webp",       // WebP
                    "image/bmp",        // BMP
                    "image/svg+xml"     // SVG
            ),
            10 * 1024 * 1024,
            true  // 需要生成预览（生成缩略图）
    ),

    //@Schema(description = "预览文件：生成预览图片，最大10MB")
    PREVIEW(
            "static/previews",
            Set.of("image/png", "image/jpeg"),  // 仅允许图片格式
            10 * 1024 * 1024,
            true
    ),

    //@Schema(description = "办公文档：支持Office系列及PDF等，最大50MB")
    DOCUMENT(
            "static/docs",
            Set.of(
                    "application/pdf",  // PDF
                    "application/msword",  // DOC
                    "application/vnd.openxmlformats-officedocument.wordprocessingml.document",  // DOCX
                    "application/vnd.ms-excel",  // XLS
                    "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",  // XLSX
                    "application/vnd.ms-powerpoint",  // PPT
                    "application/vnd.openxmlformats-officedocument.presentationml.presentation",  // PPTX
                    "text/plain",        // TXT
                    "text/csv",          // CSV
                    "application/rtf"    // RTF
            ),
            50 * 1024 * 1024,
            true  // 需要生成预览（生成缩略图）
    ),

    //@Schema(description = "用户头像：仅允许JPG/PNG格式，最大2MB")
    AVATAR(
            "user/avatar",
            Set.of(
                    "image/jpeg",
                    "image/png"
            ),
            2 * 1024 * 1024,
            false
    ),

    //@Schema(description = "音视频文件：支持常见媒体格式，最大500MB")
    MEDIA(
            "static/media",
            Set.of(
                    "audio/mpeg",       // MP3
                    "audio/wav",        // WAV
                    "video/mp4",        // MP4
                    "video/quicktime",  // MOV
                    "video/x-msvideo",  // AVI
                    "video/x-flv",      // FLV
                    "video/webm"        // WEBM
            ),
            500 * 1024 * 1024,
            false
    ),

    //@Schema(description = "压缩文件：支持ZIP/RAR等压缩格式，最大200MB")
    ARCHIVE(
            "static/archives",
            Set.of(
                    "application/zip",          // ZIP
                    "application/x-rar-compressed",  // RAR
                    "application/x-7z-compressed",  // 7Z
                    "application/x-tar",       // TAR
                    "application/gzip"          // GZ
            ),
            200 * 1024 * 1024,
            false
    );

    //@Schema(description = "OSS存储路径")
    private final String storagePath;

    //@Schema(description = "允许的MIME类型集合")
    private final Set<String> allowedMimeTypes;

    //@Schema(description = "最大文件大小（字节）")
    private final long maxFileSize;

    //@Schema(description = "是否需要生成预览图")
    private final boolean generatePreview;

    UploadStrategy(
            String storagePath,
            Set<String> allowedMimeTypes,
            long maxFileSize,
            boolean generatePreview
    ) {
        this.storagePath = storagePath;
        this.allowedMimeTypes = allowedMimeTypes;
        this.maxFileSize = maxFileSize;
        this.generatePreview = generatePreview;
    }

    //@Schema(description = "获取可读的文件大小描述")
    public String getMaxSizeDescription() {
        if (maxFileSize >= 1024 * 1024 * 1024) {
            return (maxFileSize / (1024 * 1024 * 1024)) + "GB";
        } else if (maxFileSize >= 1024 * 1024) {
            return (maxFileSize / (1024 * 1024)) + "MB";
        } else if (maxFileSize >= 1024) {
            return (maxFileSize / 1024) + "KB";
        }
        return maxFileSize + "B";
    }
}